//
//  AXFAccountManager.m
//  AIXF
//
//  Created by Macx on 2017/7/9.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AXFAccountManager.h"

@implementation AXFAccountManager

+ (instancetype)sharedManager
{
    static AXFAccountManager *instance;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        instance = [[AXFAccountManager alloc] init];
    });
    
  
    
    return instance;
    
}

- (instancetype)init
{
    if (self = [super init])
    {
         [self addObserver:self forKeyPath:@"goodsNum" options:NSKeyValueObservingOptionNew context:nil];
        
        if (_orderArray == nil)
        {
            _orderArray = [NSMutableArray array];
        }
       
    }
    return self;
}

//- (NSMutableArray<AXFHomeFirstSellModel *> *)orderArray
//{
//    if (_orderArray == nil)
//    {
//        _orderArray = [NSMutableArray array];
//    }
//    return _orderArray;
//   
//}

- (void)setOrderArray:(NSMutableArray<AXFHomeFirstSellModel *> *)orderArray
{
    
}

- (void)setGoodsNum:(NSInteger)goodsNum
{
    _goodsNum = goodsNum;
    
    __block CGFloat price = 0;
    
    [self.orderArray enumerateObjectsUsingBlock:^(AXFHomeFirstSellModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        price += [obj.price floatValue] * obj.count;
        
        if (obj.count == 0)
        {
            [self.orderArray removeObject:obj];
        }
    }];
    
    self.allPrice = [NSString stringWithFormat:@"%.2f",price];
}

- (void)setAllPrice:(NSString *)allPrice
{
    _allPrice = allPrice;
    
    //NSLog(@"%f",allPrice);
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:kChangeOrderNumNotification object:nil];
    __block CGFloat price = 0;
    
    [self.orderArray enumerateObjectsUsingBlock:^(AXFHomeFirstSellModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        price += [obj.price floatValue] * obj.count;
        
        if (obj.count == 0)
        {
            [self.orderArray removeObject:obj];
        }
        
    }];
    
    self.allPrice = [NSString stringWithFormat:@"%.2f",price];
}

@end
